/***************************************************************
|	File:		MainMenuState.cpp
|	Author:		
|	Course:		
|	Purpose:	MainMenuState class handles the main menu
***************************************************************/

#include "MainMenuState.h"

#include "../SGD Wrappers/SGD_AudioManager.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_InputManager.h"

#include "Game.h"
#include "BitmapFont.h"
#include "GameplayState.h"


/**************************************************************/
// GetInstance
//	- store the ONLY instance in global memory
//	- return the only instance by pointer
/*static*/ MainMenuState* MainMenuState::GetInstance( void )
{
	static MainMenuState s_Instance;

	return &s_Instance;
}


/**************************************************************/
// Enter
//	- reset the game & load resources
/*virtual*/ void MainMenuState::Enter( void )
{
	// Reset the cursor to the top
	// (commented out to keep the last cursor position)
	//m_nCursor = 0;

	
	// Set background color
	SGD::GraphicsManager::GetInstance()->SetClearColor( {50, 50, 50} );	// dark gray
}

/**************************************************************/
// Exit
//	- deallocate / unload resources
/*virtual*/ void MainMenuState::Exit( void )
{
}


/**************************************************************/
// Update
//	- handle input & update entites
/*virtual*/ bool MainMenuState::Update( float elapsedTime )
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();

	// Press Escape to quit
	if( pInput->IsKeyPressed( SGD::Key::Escape ) == true )
		return false;	// quit game



	// Move cursor
	if( pInput->IsKeyPressed( SGD::Key::Down ) == true )
	{
		++m_nCursor;

		// Wrap around
		if( m_nCursor > 1 )		// should enumerate the options
			m_nCursor = 0;
	}
	else if( pInput->IsKeyPressed( SGD::Key::Up ) == true )
	{
		--m_nCursor;

		// Wrap around
		if( m_nCursor < 0 )
			m_nCursor = 1;
	}


	// Select an option?
	if( pInput->IsKeyPressed( SGD::Key::Enter ) == true )
	{
		// Which option?
		if( m_nCursor == 0 )		// PLAY
		{
			// ChangeState is VERY VOLATILE!!!
			//	- can only be safely called by a game state's
			//	 Update or Render methods!
			Game::GetInstance()->ChangeState( GameplayState::GetInstance() );

			// Exit this state immediately
			return true;	// keep playing in the new state
		}
		else if( m_nCursor == 1 )	// EXIT
		{
			return false;	// quit the game
		}
	}

	return true;	// keep playing
}


/**************************************************************/
// Render
//	- render entities / menu options
/*virtual*/ void MainMenuState::Render( float elapsedTime )
{
	// Use the game's font
	const BitmapFont* pFont = Game::GetInstance()->GetFont();

	// Align text based on window width
	float width = Game::GetInstance()->GetScreenWidth();


	// Display the game title centered at 4x scale
	pFont->Draw( "GAME", { (width - (4 * 32 * 4.0f))/2, 50 }, 
				 4.0f, { 255, 255, 255 } );

	// Display the menu options centered at 1x scale
	pFont->Draw( "Play", { (width - (4 * 32))/2, 300 }, 
				 1.0f, {255, 0, 0} );
	pFont->Draw( "Exit", { (width - (4 * 32))/2, 350 }, 
				 1.0f, {255, 0, 0} );

	// Display the cursor next to the option
	pFont->Draw( "=     =", { (width - (7 * 32))/2, 300.0f + 50*m_nCursor }, 
				 1.0f, {0, 255, 0} );

}
